package com.beiding.workordersystem.system.service.impl;

import com.beiding.workordersystem.common.service.BaseService;
import com.beiding.workordersystem.common.utils.QueryUtils;
import com.beiding.workordersystem.system.dao.VariableDao;
import com.beiding.workordersystem.system.entity.Variable;
import com.beiding.workordersystem.system.service.VariableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class VariableServiceImpl extends BaseService implements VariableService {
    @Autowired
    private VariableDao variableDao;

    @Override
    public String getValue(String key) {
        Variable selectOne = variableDao.selectOne(QueryUtils.getQuery(Variable.class).eq(Variable::getName, key));
        if (selectOne == null) {
            return null;
        } else return selectOne.getValue();
    }

    @Override
    public String set(String key, String value) {

        Variable selectOne = variableDao.selectOne(QueryUtils.getQuery(Variable.class).eq(Variable::getName, key));
        if (selectOne == null) {
            if (value != null) {
                Variable variable = new Variable();
                variable.setName(key);
                variable.setValue(value);
                variableDao.insert(variable);
            }
            return null;
        } else {
            if (value == null) {
                variableDao.delete(QueryUtils.getQuery(Variable.class).eq(Variable::getName, key));
            } else {
                selectOne.setValue(value);
                variableDao.update(selectOne, QueryUtils.getQuery(Variable.class).eq(Variable::getName, key));
            }
            return selectOne.getValue();
        }
    }

}
